﻿<cfcomponent name="data" ExtDirect="true">
	
<cffunction name="eventRead" ExtDirect="true">
<cfargument name="eventdata" >


<cfset calendardata = ORMExecuteQuery("from GINCALENDAR 
                                      where STARTDATE >= '#eventdata.startDate#' 
                                            and ENDDATE <= '#eventdata.endDate#'") >
                                            
<cfset jsonrepresent = StructNew() >
<cfset dataArr = arraynew(1) >
<cfset cntArr = 1 >                                     
<cfloop array="#calendardata#" index="calIndex" >
	<cfset middlejson = structnew() >
	<cfset middlejson['id']    	= calIndex.getEVENTID() >
	<cfset middlejson['cid'] 	= calIndex.getCALENDARID() >
	<cfset middlejson['title']  = calIndex.getTITLE() >
	<cfset middlejson['start']  = dateformat(calIndex.getSTARTDATE(), 'YYYY-MM-DD') & ' ' & timeformat(calIndex.getSTARTDATE(), 'HH:MM:SS') >
	<cfset middlejson['end'] 	= dateformat(calIndex.getENDDATE(), 'YYYY-MM-DD') & ' ' & timeformat(calIndex.getENDDATE(), 'HH:MM:SS') >
	<cfset middlejson['rrule'] 	= calIndex.getRECURRULE() >
	<cfset middlejson['loc'] 	= calIndex.getLOCATION() >
	<cfset middlejson['notes'] 	= calIndex.getNOTES() >
	<cfset middlejson['url'] 	= calIndex.getURL() >
	<cfset middlejson['ad'] 	= calIndex.getISALLDAY() >
	<cfset middlejson['rem'] 	= calIndex.getREMINDER() >
	<cfset dataArr[cntArr] = middlejson >
	<cfset cntArr = cntArr + 1 >
</cfloop>

<cfset jsonrepresent['data'] = dataArr >
<cfreturn jsonrepresent />

<cfsetting showdebugoutput="false" >
</cffunction>
	

<cffunction name="eventCreate" ExtDirect="true">
<cfargument name="eventdata" >
<cfif isArray(eventdata) >
	<cfset eventdata = eventdata[1] >
</cfif>
<cfset ad = eventdata.ad >
<cfset cid = eventdata.cid >
<cfset l_end = left(eventdata.end, 19) >
<cfset endDatepart = left(l_end, 10) >
<cfset endTimepart = right(l_end, 8) >
<cfset end = endDatepart & ' ' & endTimepart >
<cfset id = eventdata.id >
<cfset loc = eventdata.loc >
<cfset notes = eventdata.notes >
<cfset rem = eventdata.rem >
<cfset rrule = eventdata.rrule >
<cfset l_start = left(eventdata.start, 19) >
<cfset startDatepart = left(l_start, 10) >
<cfset startTimepart = right(l_start, 8) >
<cfset start = startDatepart & ' ' & startTimepart >
<cfset title = eventdata.title >
<cfset urlstring = eventdata.url >


<cfset calendartable = EntityNew("GINCALENDAR") >
<cfset calendartable.setCALENDARID("#cid#") >
<cfset calendartable.setTITLE("#title#") >
<cfset calendartable.setSTARTDATE("#start#") >
<cfset calendartable.setENDDATE("#end#") >
<cfset calendartable.setRECURRULE("#rrule#") >
<cfset calendartable.setLOCATION("#loc#") >
<cfset calendartable.setNOTES("#notes#") >
<cfset calendartable.setURL("#urlstring#") >
<cfset calendartable.setISALLDAY("#ad#") >
<cfset calendartable.setREMINDER("#rem#") >
<cfset calendartable.setOWNER("#client.admin#") >
<cfset calendartable.setDATELASTUPDATE("#dateformat(now(), 'YYYY-MM-DD')# #timeformat(now(), 'HH:MM:SS')#") >

<cfset EntitySave(calendartable) >
<cfset ormflush()>

<cfinvoke method="newschedule" argumentcollection="#eventdata#" >

<cfset jsonrepresent = StructNew() >
<cfset dataArr = arraynew(1) >

<cfset middlejson = structnew() >
<cfset middlejson['id'] = id >
<cfset middlejson['cid'] = cid >
<cfset middlejson['title'] = title >
<cfset middlejson['start'] = start >
<cfset middlejson['end'] = end >
<cfset middlejson['rrule'] = rrule >
<cfset middlejson['loc'] = loc >
<cfset middlejson['notes'] = notes >
<cfset middlejson['url'] = urlstring >
<cfset middlejson['ad'] = ad >
<cfset middlejson['rem'] = rem >
<cfset dataArr[1] = middlejson >


<cfset jsonrepresent['data'] = dataArr >
<cfreturn jsonrepresent />

<cfsetting showdebugoutput="false" >
</cffunction>


<cffunction name="eventUpdate" ExtDirect="true">
<cfargument name="eventdata" >

<cfif isArray(eventdata) >
	<cfset eventdata = eventdata[1] >
</cfif>
<cfset ad = eventdata.ad >
<cfset cid = eventdata.cid >
<cfset l_end = left(eventdata.end, 19) >
<cfset endDatepart = left(l_end, 10) >
<cfset endTimepart = right(l_end, 8) >
<cfset end = endDatepart & ' ' & endTimepart >
<cfset id = eventdata.id >
<cfset loc = eventdata.loc >
<cfset notes = eventdata.notes >
<cfset rem = eventdata.rem >
<cfset rrule = eventdata.rrule >
<cfset l_start = left(eventdata.start, 19) >
<cfset startDatepart = left(l_start, 10) >
<cfset startTimepart = right(l_start, 8) >
<cfset start = startDatepart & ' ' & startTimepart >
<cfset title = eventdata.title >
<cfset urlstring = eventdata.url >

<cfset calendartable = EntityLoad("GINCALENDAR", id, true ) >

<cfset calendartable.setCALENDARID("#cid#") >
<cfset calendartable.setTITLE("#title#") >
<cfset calendartable.setSTARTDATE("#start#") >
<cfset calendartable.setENDDATE("#end#") >
<cfset calendartable.setRECURRULE("#rrule#") >
<cfset calendartable.setLOCATION("#loc#") >
<cfset calendartable.setNOTES("#notes#") >
<cfset calendartable.setURL("#urlstring#") >
<cfset calendartable.setISALLDAY("#ad#") >
<cfset calendartable.setREMINDER("#rem#") >
<cfset calendartable.setOWNER("#client.admin#") >
<cfset calendartable.setDATELASTUPDATE("#dateformat(now(), 'YYYY-MM-DD')# #timeformat(now(), 'HH:MM:SS')#") >

<cfset EntitySave(calendartable) >
<cfset ormflush()>

<cfinvoke method="newschedule" argumentcollection="#eventdata#" >

<cfset jsonrepresent = StructNew() >
<cfset dataArr = arraynew(1) >

<cfset middlejson = structnew() >
<cfset middlejson['id'] = id >
<cfset middlejson['cid'] = cid >
<cfset middlejson['title'] = title >
<cfset middlejson['start'] = start >
<cfset middlejson['end'] = end >
<cfset middlejson['rrule'] = rrule >
<cfset middlejson['loc'] = loc >
<cfset middlejson['notes'] = notes >
<cfset middlejson['url'] = urlstring >
<cfset middlejson['ad'] = ad >
<cfset middlejson['rem'] = rem >
<cfset dataArr[1] = middlejson >


<cfset jsonrepresent['data'] = dataArr >
<cfreturn jsonrepresent />

<cfsetting showdebugoutput="false" >
</cffunction>


<cffunction name="eventDestroy" ExtDirect="true">
<cfargument name="eventdata" >
<cfif isArray(eventdata) >
	<cfset eventdata = eventdata[1] >
</cfif>
<cfset ad = eventdata.ad >
<cfset cid = eventdata.cid >
<cfset l_end = left(eventdata.end, 19) >
<cfset endDatepart = left(l_end, 10) >
<cfset endTimepart = right(l_end, 8) >
<cfset end = endDatepart & ' ' & endTimepart >
<cfset id = eventdata.id >
<cfset loc = eventdata.loc >
<cfset notes = eventdata.notes >
<cfset rem = eventdata.rem >
<cfset rrule = eventdata.rrule >
<cfset l_start = left(eventdata.start, 19) >
<cfset startDatepart = left(l_start, 10) >
<cfset startTimepart = right(l_start, 8) >
<cfset start = startDatepart & ' ' & startTimepart >
<cfset title = eventdata.title >
<cfset urlstring = eventdata.url >

<cfset calendartable = EntityLoad("GINCALENDAR", id, true ) >
<cfset EntityDelete(calendartable) >
<cfset ormflush()>

<cfset jsonrepresent = StructNew() >
<cfset dataArr = arraynew(1) >
<cfset middlejson = structnew() >
<cfset middlejson['id'] = id >
<cfset middlejson['cid'] = cid >
<cfset middlejson['title'] = title >
<cfset middlejson['start'] = start >
<cfset middlejson['end'] = end >
<cfset middlejson['rrule'] = rrule >
<cfset middlejson['loc'] = loc >
<cfset middlejson['notes'] = notes >
<cfset middlejson['url'] = urlstring >
<cfset middlejson['ad'] = ad >
<cfset middlejson['rem'] = rem >
<cfset dataArr[1] = middlejson >
<cfset jsonrepresent['data'] = dataArr >

<cftry>
<cfschedule 
    action = "delete"
    task = "#title##start#">
	<cfcatch>
	</cfcatch>
</cftry>
<cfreturn jsonrepresent />
<cfsetting showdebugoutput="false" >
</cffunction>


<cffunction name="newschedule" returntype="void" >

<cfargument name="ad" >
<cfargument name="cid" >
<cfargument name="end" >
<cfargument name="loc" > 
<cfargument name="notes" > 
<cfargument name="rem" > 
<cfargument name="rrule" > 
<cfargument name="start" > 
<cfargument name="title" > 
<cfargument name="urlstring" > 


	
<cfif rem NEQ "" > <!--- The reminder is set to None. None means no reminder. The event saved is treated as a note. --->
<cfset l_end = left(end, 19) >
<cfset endDatepart = left(l_end, 10) >
<cfset endTimepart = right(l_end, 8) >
<cfset end = endDatepart & ' ' & endTimepart >

<cfset l_start = left(start, 19) >
<cfset startDatepart = left(l_start, 10) >
<cfset startTimepart = right(l_start, 8) >
<cfset startA = startDatepart & ' ' & startTimepart >
<cfset startN = dateadd("n", rem * -1, startA) > 
<cfset startDatepart = dateformat(startN, 'YYYY-MM-DD') >
<cfset startTimepart = timeformat(startN, 'HH:MM:SS' ) >

	<cfschedule 
	    action = "update"
	    task = "#title##start#"
	    requestTimeOut = "100"
	    resolveURL = "yes"overwrite = "yes"
	    startDate = "#startDatepart#"
	    startTime = "#startTimepart#"
	    url = "#client.domain#myapps/calendar/schedule/notify.cfm?id=#urlencodedformat(id)#&ad=#urlencodedformat(ad)#&cid=#urlencodedformat(cid)#&end=#urlencodedformat(end)#&loc=#urlencodedformat(loc)#&notes=#urlencodedformat(notes)#&rem=#urlencodedformat(rem)#&rrule=#urlencodedformat(rrule)#&start=#urlencodedformat(startA)#&title=#urlencodedformat(title)#&urlstring=#urlencodedformat(urlstring)#&recipient=#urlencodedformat(client.profilename)#"
		interval="once" 
		operation="HTTPRequest"
	>
</cfif>
	
</cffunction>
	
</cfcomponent>